package com.xxwu.springlibrary.controller;

import com.xxwu.springlibrary.contans.Contans;
import com.xxwu.springlibrary.model.*;
import com.xxwu.springlibrary.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/BookController")
public class BookController {
    //使用Ioc完成对Book类的解耦
    @Autowired
    private BookService bookService;
    @RequestMapping("/getList")
    //从服务层取数据与前端进行数据交换
    public Result<PageResult<BookInfo>> getList(PageRequest pageRequest) {
        log.info("pageRequest{}, 请求信息" + pageRequest);
        if(pageRequest.getPageCurrent() < 1){
            return Result.fail("查询失败");
        }
        //若登录成功，将pageResult传入Result的success方法中，作为成功结果返回
        PageResult<BookInfo> pageResult = bookService.getBookList(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping(value = "/insertBook", produces = "application/json")
    /**
     * 由于这里的返回类型为String，经统一结果返回处理后，返回到前端的数据将会变成text
     * 此时前端将无法解析result.code等等内容，页面处理失败，但后端的信息仍可添加成功
     * 处理方法
     * 1 将RequestMapping的返回类型修改为application/json,但仍要在统一结果返回时进行字符串处理
     * 2 不使用String作为返回类型,永绝后患
     */
    //添加图书信息
    public String addBook(BookInfo book){
        if(!StringUtils.hasLength(book.getBookName())
        || !StringUtils.hasLength(book.getAuthor())
        || !StringUtils.hasLength(book.getPublish())
        || book.getCount() <= 0
        || book.getPrice() == null){
            return "error";
        }
        bookService.addBook(book);
        return "success";
    }

    @RequestMapping("/getBookById")
    //根据Id查询图书
    public BookInfo getBookById(Integer bookId){
        log.info("getBookById" + bookId);
        BookInfo bookInfo = bookService.getBookById(bookId);
        return bookInfo;
    }

    //修改/逻辑删除图书
    @RequestMapping("/updateBooks")
    public Result updateBooks(BookInfo book){
        //对输入的图书信息进行校验
        if(book.getId() < 1){
            return Result.fail(false,"id不合法");
        }
        try{
            log.info("updateBooks" + book);
            Integer result = bookService.updateBooks(book);
            //若前端传入数据失败，导致后端update = 0，并没有修改成功，但sql仍然执行成功
            //此时修改操作不会报错但也不会修改成功
            if(result <= 0){
                return Result.fail(false,"更新失败");
            }
            return Result.success(true);
        }catch(Exception e){
            log.error("更新失败");
            return Result.fail(false, e.getMessage());
        }
    }

    //批量删除操作
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids){
        try{
            log.info("batchDelete" + ids);
            Integer result = bookService.batchDelete(ids);
            if(result <= 0){
                return false;
            }
        }catch(Exception e){
            log.info("删除失败 e{}" + e.getMessage());
            return false;
        }
        return true;
    }


}
